<?php
class Company_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }
	
	function get_rmname_from_id($id){
		$this->db->select('name_roman')->where('id', $id);
		$query = $this->db->get("company",1);
		
		$rs = $query->result();
		if ($rs) return $rs[0]->name_roman;
		return -1;
	}
	
	function get_id_from_name($name_roman){
		$this->db->select('id')->where('name_roman', $name_roman);
		$this->db->where('delete_tag !=', '2');
		
		$query = $this->db->get("company",1);
		
		$rs = $query->result();
		if ($rs) return $rs[0]->id;
		return -1;
	}
	
	function get_testid_from_name($name_roman){
		$this->db->select('id')->where('name_roman', $name_roman);
		$this->db->where('delete_tag', '2');
		
		$query = $this->db->get("company",1);
		
		$rs = $query->result();
		if ($rs) return $rs[0]->id;
		return -1;
	}
	function get_testid_from_id($id){
		$name_roman = $this->get_rmname_from_id($id);
		return $this->get_testid_from_name($name_roman);
	}
	
	function get_company_info($id){
		$this->db->where('id', $id);
		$rs = $this->db->get("company",1)->result();
		
		//$rs[0]->images = explode(",", $rs[0]->images);
		//show_error($rs[0]->images);
		return $rs[0];
	}
	
	function get_company_meta_info($id, $select=""){
		
		if ($select != "") $this->db->select($select);
		
		$this->db->where('company_id', $id);
		$rs = $this->db->get("company_meta",1)->result();
		
		//show_error($id);
		
		return $rs[0];
	}
	
	function get_company_list(){
		$this->db->where('delete_tag', 0);
		
		$this->db->select('id, kaisha_mei, address, type');
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
	function get_del_list(){
		$this->db->where('delete_tag', 1);
		
		$this->db->select('id, kaisha_mei, address, type');
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
	// get all table fields
	function get_table_field($table_name){
		$fields = $this->db->list_fields($table_name);
		return $fields;
	}
	
	function get_table_field_data($table_name){
		$fields = $this->db->field_data($table_name);
		
		// change to array
		foreach ($fields as &$field) {
			$field = (array)$field;
		}
		
		return $fields;
	}
	
	/*
	 * Get column type (int, string, blob)
	 */
	function get_column_type(){
		$type = array();
		$company_field = $this->get_table_field_data("company");
		$company_meta_field = $this->get_table_field_data("company_meta");
		
		foreach ($company_field as $field) {
			$type[ $field["name"] ] = $field["type"];
		}
		foreach ($company_meta_field as $field) {
			$type[ $field["name"] ] = $field["type"];
		}
		
		return $type;
	}
	
	// update
	function update_company($id, $company, $company_meta){
		//company
		$this->db->where('id', $id);
		$this->db->update('company', $company); 
		
		//company_meta
		$this->db->where('company_id', $id);
		return $this->db->update('company_meta', $company_meta); 
	}
	
	// insert
	function insert_company($company, $company_meta){
		//company
		$this->db->insert('company', $company); 
		$id = $this->db->insert_id();
		//company_meta
		$company_meta["company_id"] = $id;
		$this->db->insert('company_meta', $company_meta);
		
		return $id;
	}
	
	function mark_del_company($id){
		$this->db->where('id', $id);
		return $this->db->update('company', array("delete_tag" => 1)); 
	}
	
	function mark_undel_company($id){
		$this->db->where('id', $id);
		return $this->db->update('company', array("delete_tag" => 0)); 
	}

	// retrieve images
	function retrieve_images($id){
		//$this->db->select('images');
		//$this->db->where('id', $id);
		//$rs = $this->db->get("company")->result();
		
		//return $rs;
	}
	
	//
	function get_list_type($type){
		$this->db->select("id, kaisha_mei, logo_img, name_roman, address, type, images, img_cmt");
		$this->db->where('delete_tag', 0);
		$this->db->where("type like '%$type%'");
		
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
	function get_list_area($areaid){
		$this->db->select("id, kaisha_mei, logo_img, name_roman, address, type, images, img_cmt");
		$this->db->where('delete_tag', 0);
		$this->db->where("area_id like '%$areaid%'");
		
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
	function get_list_what($whatid){
		$this->db->select("id, kaisha_mei, logo_img, name_roman, address, type, images, img_cmt");
		$this->db->where('delete_tag', 0);
		$this->db->where("what_id like '%$whatid%'");
		
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
	function get_list_all(){
		$this->db->where('delete_tag', 0);
		
		$this->db->select("id, kaisha_mei, logo_img, name_roman, address, type, images, img_cmt");
		$rs = $this->db->get("company")->result();
		
		return $rs;
	}
	
}